%  This file is part of Erlywog.

%  Erlywog is free software: you can redistribute it and/or modify
%  it under the terms of the GNU General Public License as published by
%  the Free Software Foundation, either version 3 of the License, or
%  (at your option) any later version.

%  Erlywog is distributed in the hope that it will be useful,
%  but WITHOUT ANY WARRANTY; without even the implied warranty of
%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%  GNU General Public License for more details.

%  You should have received a copy of the GNU General Public License
%  along with Erlywog.  If not, see <http://www.gnu.org/licenses/>.

%  Copyright Jasm Sison 2009

-module(image_manager).
-include_lib("eunit/include/eunit.hrl"). %% turn on unit tests
-export([
	boot/0
	, start/0
	, stop/0
        , get_random_image_set/0
	, get_image_location/1
	]).

% macros
-define(SALT1, "aoeuaoeuaoeuaoeuaeouaoeuaeuaeuaueaouaoeuaoeaoeuaoeu").
-define(SALT2, "asadsadsfasdfasfasfasfdsafsafdsafdsfsfdaffdsdfsfdsd").
-define(UNIFORMSEED, {432123,897097097,986123483902101}).
-define(tablename, image_sets_table).
-define(processname, image_sets_process).
-define(IMAGE_GROUPS_SIZE, 10).
-define(prefix, "/images/").
-define(IMAGE_GROUPS,[
  ["200.0.jpeg","200.100.jpeg","200.200.jpeg","200.300.jpeg"]
, ["200.400.jpeg","200.500.jpeg","200.600.jpeg","200.700.jpeg"]
, ["300.0.jpeg","300.100.jpeg","300.200.jpeg","300.300.jpeg"]
, ["300.400.jpeg","300.500.jpeg","300.600.jpeg","300.700.jpeg"]
, ["400.0.jpeg","400.100.jpeg","400.200.jpeg","400.300.jpeg"]
, ["400.400.jpeg","400.500.jpeg","400.600.jpeg","400.700.jpeg"]
, ["500.0.jpeg","500.100.jpeg","500.200.jpeg","500.300.jpeg"]
, ["500.400.jpeg","500.500.jpeg","500.600.jpeg","500.700.jpeg"]
, ["600.0.jpeg","600.100.jpeg","600.200.jpeg","600.300.jpeg"]
, ["600.400.jpeg","600.500.jpeg","600.600.jpeg","600.700.jpeg"]
, ["700.0.jpeg","700.100.jpeg","700.200.jpeg","700.300.jpeg"]
, ["700.400.jpeg","700.500.jpeg","700.600.jpeg","700.700.jpeg"]
]).

%% TODO + add macros to store image locations etc.
%% TODO + add hashed (names of images + supersecretkey) to ets
%% TODO + make supersecret key a macro
%% TODO - move macros and blog parameters to blog.hrl and import to here
%% TODO + move shuffle elsewhere, to blog_util maybe...
	
boot() ->
  start().
  
start() ->
  Fun = fun() -> loop(ets:new(?processname, [set, private])) end
  , process_util:make_process(?processname, Fun)
  , Elements = lists:zipwith(fun(A,B) -> {A,B} end, lists:seq(1,length(?IMAGE_GROUPS)), ?IMAGE_GROUPS)
  , insert(Elements).

stop() ->
  destroy_table_and_process().

insert(X) ->
  ?processname ! {self(), insert, X}.

lookup(K) ->
  ?processname ! {self(), lookup, K},
  receive_message().

destroy_table_and_process() ->
  ?processname ! {self(), kill},
  receive_message().

receive_message() ->
  receive
    All ->
      All
  end.

loop(Tid) ->
  receive
    {_, insert, X} ->
      ets:insert(Tid, X)
      , loop(Tid);
    {From, lookup, K} ->
      From ! ets:lookup(Tid, K),
      loop(Tid);
    {From, kill} ->
      From ! ets:delete(Tid);
    _ ->
      loop(Tid)
  end.

store_hash_plus_image_location(ImageLocation) -> 
  Hash = md5:hex([?SALT1, ImageLocation, ?SALT2]), 
  insert({Hash, [?prefix, ImageLocation]}),
  Hash.

seed_uniform() ->
  {Aseed,Bseed,Cseed} = now()
  , {Dseed,Eseed,Fseed} = ?UNIFORMSEED
  , random:seed(Aseed+Dseed,Bseed+Eseed,Cseed+Fseed).

get_random_image_set() ->
  seed_uniform()  
  , RandomNumber = random:uniform(?IMAGE_GROUPS_SIZE)
%  , io:format("~p~n",[RandomNumber]) %% test
  , [{_, IMAGE_GROUP}] = lookup(RandomNumber)
  , [ store_hash_plus_image_location(Image) || Image <- IMAGE_GROUP ].

get_image_location(Hex) ->
  case lookup(Hex) of
    [{_, ImageLocation}] ->
      ImageLocation;
    [] ->
      not_found
  end.

